package io.renren.common.utils;

import io.renren.modules.areainfos.service.AreainfosService;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: stf
 * @description: 分页默认值工具类
 * @author: Custom
 * @create: 2018-11-09 15:42
 **/

public class CustomUtils {

    private static final Pattern PATTERN = Pattern.compile("[0-9]*");

    public static void judgePageParams(Map<String, Object> params){
        if(params.containsKey("limit")){
            int limit = Integer.parseInt((String) params.get("limit"));
            if(limit > 30){
                params.put("limit","10");
            }
        }else {
            params.put("limit","10");
        }
        if (!params.containsKey("page")) {
            params.put("page","1");
        }
        if (params.containsKey("order")) {
            String order = (String) params.get("order");
            if (!"asc".equals(order) && !"desc".equals("order")) {
                params.put("order","asc");
            }
        }else {
            params.put("order","asc");
        }
        if (!params.containsKey("sidx")) {
            params.put("sidx","createtime");
        }
    }

    public static void setSearchByArea(Map<String, Object> params, AreainfosService areainfosService){
        String areaid;
        if(params.containsKey("areaid")){
            String columnid = (String) params.get("areaid");
            //根据id查询地区号（地区号不是主键）
            areaid = areainfosService.queryAreaidByColumnId(columnid);
            params.put("areaid",areaid);
            //判断地区为那一级
            int anInt = Integer.parseInt(areaid);
            //县/区
            int xian = anInt % 100;
            int shi = anInt % 10000;
            int shen = anInt % 1000000;
            if(xian > 0){
                //地区为县级
                anInt += 1;
            }else if(shi > 0){
                //为市级
//                shi += 1000;
                anInt += 1000;
            }else if(shen > 0){
                //为省级
//                shen += 10000;
                anInt += 10000;
            }
            areaid = String.valueOf(anInt);
            params.put("areaidend",areaid);
        }
    }

    /*
     * 判断是否为整数
     * @param str 传入的字符串
     * @return 是整数返回true,否则返回false
     */

    public static boolean isInteger(String str) {
        return PATTERN.matcher(str).matches();
    }

}
